import { useState, useEffect, useCallback } from 'react';
import { message } from 'antd';
import { request } from '@/utils/request';
import type { 
  AIPerson, 
  Pagination, 
  AIPersonListParams, 
  AIPersonListResponse,
  Language,
  PersonType 
} from '../types';

// AI人员列表管理Hook
export const useAIPersonList = () => {
  const [loading, setLoading] = useState(false);
  const [personList, setPersonList] = useState<AIPerson[]>([]);
  const [pagination, setPagination] = useState<Pagination>({
    total: 0,
    page: 1,
    pageSize: 128,
    totalPages: 1
  });

  // 获取AI人员列表数据
  const fetchPersonList = useCallback(async (params: AIPersonListParams) => {
    setLoading(true);
    try {
      const response = await request.post<AIPersonListResponse>('/api/ai-persons/', params);
      
      if (response.data.success) {
        setPersonList(response.data.data.list);
        setPagination(response.data.data.pagination);
      } else {
        message.error('获取数据失败');
      }
    } catch (error) {
      console.error('获取AI人员列表失败:', error);
      message.error('获取数据失败，请重试');
    } finally {
      setLoading(false);
    }
  }, []);

  // 刷新列表数据
  const refreshList = useCallback((language: Language, type: PersonType, page = 1) => {
    const params: AIPersonListParams = {
      page,
      pageSize: 128,
      type,
      information: false,
      language
    };
    fetchPersonList(params);
  }, [fetchPersonList]);

  return {
    loading,
    personList,
    pagination,
    refreshList
  };
}; 